package com.csdj.t309springboot.service;
import com.csdj.t309springboot.entity.User;
import com.csdj.t309springboot.entity.UserExample;
import com.csdj.t309springboot.mapper.UserMapper;
import com.csdj.t309springboot.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserService implements IUserService{
    public UserService() {
        System.out.println("初始化UserService()");
    }
    @Resource
    UserMapper mapper;
    //@PostConstruct 容器加载完之后自动执行标注的方法

    @Override
    public User login(String account,String password){
        UserExample userExample = new UserExample();
        userExample.createCriteria().andAccountEqualTo(account).andPasswordEqualTo(SecurityUtils.md5Hex(password));
        List<User> list = mapper.selectByExample(userExample);
        System.out.println("size:"+list.size());
        if(list != null && list.size()>0 ){
            System.out.println("==登录成功==");
            return list.get(0);
        }
        System.out.println("< 登录失败 >");
        return null;
    }

    @Override
    public User register(String account, String password) {
        User user = new User();
        user.setAccount(account);
        user.setPassword(SecurityUtils.md5Hex(password));
        user.setRoleId(1);
        int insert = mapper.insert(user);
        if( insert > 0 ){
            System.out.println("==注册成功==");
            return user;
        }
        System.out.println("< 注册失败 >");
        return null;
    }
    @Override
    public List<User> getAllUsers() {
        return mapper.selectByExample(new UserExample());
    }

    

}
